home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / langs / xlisp2.1 / xldist02.zoo / sources / ststuff.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-05-03  |  5.1 KB  |  282 lines

  1. /* $Header: ststuff.c,v 1.1 91/03/15 14:42:26 gjh Exp $ */
  2. /* ststuff.c - atari-st specific routines */
  3.  
  4. #include <osbind.h>
  5. #include "xlisp.h"
  6.  
  7. #define LBSIZE 200
  8.  
  9. /* set MWC memory parameters */
  10. long _stksize = 16384;    /* stack must be 16K */
  11.  
  12. /* external variables */
  13. extern LVAL s_unbound,true;
  14. extern int errno;
  15. extern FILE *tfp;
  16. extern char buf[];
  17.  
  18. /* line buffer variables */
  19. static char lbuf[LBSIZE];
  20. static int  lpos[LBSIZE];
  21. static int lindex;
  22. static int lcount;
  23. static int lposition;
  24.  
  25. /* osinit - initialize */
  26. osinit(banner)
  27.   char *banner;
  28. {
  29.     printf("\033v%s\n",banner);
  30.     lposition = 0;
  31.     lindex = 0;
  32.     lcount = 0;
  33. }
  34.  
  35. /* osfinish - clean up before a return to the operating system */
  36. osfinish()
  37. {
  38. }
  39.  
  40. /* oserror - print an error message */
  41. oserror(msg)
  42.   char *msg;
  43. {
  44.     printf("error: %s\n",msg);
  45. }
  46.  
  47. /* osrand - return a random number between 0 and n-1 */
  48. int osrand(n)
  49.   int n;
  50. {
  51.     return (rand() % n);
  52. }
  53.  
  54. /* osaopen - open an ascii file */
  55. FILE *osaopen(name,mode)
  56.   char *name,*mode;
  57. {
  58.     return (fopen(name,mode));
  59. }
  60.  
  61. /* osbopen - open a binary file */
  62. FILE *osbopen(name,mode)
  63.   char *name,*mode;
  64. {
  65.     char rmode[5];
  66.     strcpy(rmode,mode); strcat(rmode,"b");
  67.     return (fopen(name,rmode));
  68. }
  69.  
  70. /* osclose - close a file */
  71. int osclose(fp)
  72.   FILE *fp;
  73. {
  74.     return (fclose(fp));
  75. }
  76.  
  77. /* osagetc - get a character from an ascii file */
  78. int osagetc(fp)
  79.   FILE *fp;
  80. {
  81.     return (getc(fp));
  82. }
  83.  
  84. /* osaputc - put a character to an ascii file */
  85. int osaputc(ch,fp)
  86.   int ch; FILE *fp;
  87. {
  88.     return (putc(ch,fp));
  89. }
  90.  
  91. /* osbgetc - get a character from a binary file */
  92. int osbgetc(fp)
  93.   FILE *fp;
  94. {
  95.     return (getc(fp));
  96. }
  97.  
  98. /* osbputc - put a character to a binary file */
  99. int osbputc(ch,fp)
  100.   int ch; FILE *fp;
  101. {
  102.     return (putc(ch,fp));
  103. }
  104.  
  105. /* ostgetc - get a character from the terminal */
  106. int ostgetc()
  107. {
  108.     int ch;
  109.  
  110.     /* check for a buffered character */
  111.     if (lcount--)
  112.     return (lbuf[lindex++]);
  113.  
  114.     /* get an input line */
  115.     for (lcount = 0; ; )
  116.     switch (ch = xgetc()) {
  117.     case '\r':
  118.         lbuf[lcount++] = '\n';
  119.         xputc('\r'); xputc('\n'); lposition = 0;
  120.         if (tfp)
  121.             for (lindex = 0; lindex < lcount; ++lindex)
  122.             osaputc(lbuf[lindex],tfp);
  123.         lindex = 0; lcount--;
  124.         return (lbuf[lindex++]);
  125.     case '\010':
  126.     case '\177':
  127.         if (lcount) {
  128.             lcount--;
  129.             while (lposition > lpos[lcount]) {
  130.             xputc('\010'); xputc(' '); xputc('\010');
  131.             lposition--;
  132.             }
  133.         }
  134.         break;
  135.     case '\032':
  136.         xflush();
  137.         return (EOF);
  138.     default:
  139.         if (ch == '\t' || (ch >= 0x20 && ch < 0x7F)) {
  140.             lbuf[lcount] = ch;
  141.             lpos[lcount] = lposition;
  142.             if (ch == '\t')
  143.             do {
  144.                 xputc(' ');
  145.             } while (++lposition & 7);
  146.             else {
  147.             xputc(ch); lposition++;
  148.             }
  149.             lcount++;
  150.         }
  151.         else {
  152.             xflush();
  153.             switch (ch) {
  154.                     case '\002':        xlbreak("CTL-b",TRUE);  /* control-b */
  155.             case '\003':    xltoplevel();        /* control-c */
  156.             case '\007':    xlcleanup();        /* control-g */
  157.             case '\020':    xlcontinue();        /* control-p */
  158.             case '\032':    return (EOF);        /* control-z */
  159.             default:        return (ch);
  160.             }
  161.         }
  162.     }
  163. }
  164.  
  165. /* ostputc - put a character to the terminal */
  166. ostputc(ch)
  167.   int ch;
  168. {
  169.     /* check for control characters */
  170.     oscheck();
  171.  
  172.     /* output the character */
  173.     if (ch == '\n') {
  174.     xputc('\r'); xputc('\n');
  175.     lposition = 0;
  176.     }
  177.     else {
  178.     xputc(ch);
  179.     lposition++;
  180.    }
  181.  
  182.    /* output the character to the transcript file */
  183.    if (tfp)
  184.     osaputc(ch,tfp);
  185. }
  186.  
  187. /* oscheck - check for control characters during execution */
  188. VOID oscheck()
  189. {
  190.     int ch;
  191.     if (ch = xcheck())
  192.     switch (ch) {
  193.     case '\002':    xflush(); xlbreak("BREAK",s_unbound); break;
  194.     case '\003':    xflush(); xltoplevel(); break;
  195.     }
  196. }
  197.  
  198. /* osflush - flush the input line buffer */
  199. osflush()
  200. {
  201.     lindex = lcount = 0;
  202. }
  203.  
  204. /* xflush - flush the input line buffer */
  205. static xflush()
  206. {
  207.     ostputc('\n');
  208.     osflush();
  209. }
  210.  
  211. /* xgetc - get a character from the terminal without echo */
  212. static int xgetc()
  213. {
  214.     int ch;
  215.     while ((ch = Crawio(0xFF)) == 0)
  216.     ;
  217.     return (ch & 0xFF);
  218. }
  219.  
  220. /* xputc - put a character to the terminal */
  221. static xputc(ch)
  222.   int ch;
  223. {
  224.     Crawio(ch);
  225. }
  226.  
  227. /* xcheck - check for a character */
  228. static int xcheck()
  229. {
  230.     return (Crawio(0xFF));
  231. }
  232.  
  233. /* file name extension table */
  234. char *ext[] = { ".prg",".tos",".ttp",NULL };
  235.  
  236. /* xsystem - the built-in function 'system' */
  237. LVAL xsystem()
  238. {
  239.     char *str,*p,cmd[100];
  240.     int cmdlen,sts,i;
  241.  
  242.     /* get the command string */
  243.     str = getstring(xlgastring());
  244.     xllastarg();
  245.  
  246.     /* get the command name */
  247.     for (p = cmd, cmdlen = 0; *str && !isspace(*str); ++cmdlen)
  248.     *p++ = *str++;
  249.     *p = '\0';
  250.  
  251.     /* skip spaces between the command name and the arguments */
  252.     while (*str && isspace(*str))
  253.     ++str;
  254.  
  255.     /* make a counted ascii argument list */
  256.     for (p = &buf[1], buf[0] = '\0'; *str; ++buf[0])
  257.     *p++ = *str++;
  258.     *p = '\0';
  259.  
  260.     /* try each extension */
  261.     for (i = 0; ext[i]; ++i) {
  262.     strcpy(&cmd[cmdlen],ext[i]);
  263.     if ((sts = Pexec(0,cmd,buf,"")) != -33)
  264.         break;
  265.     }
  266.  
  267.     /* return the completion status */
  268.     return (cvfixnum((FIXTYPE)sts));
  269. }
  270.  
  271. /* xgetkey - get a key from the keyboard */
  272. LVAL xgetkey()
  273. {
  274.     xllastarg();
  275.     return (cvfixnum((FIXTYPE)xgetc()));
  276. }
  277.  
  278. /* ossymbols - lookup important symbols */
  279. ossymbols()
  280. {
  281. }
  282.